<?php

use Faker\Generator as Faker;
use App\Models\CouponCode;

$factory->define(CouponCode::class, function (Faker $faker) {
    // 随机取得一个优惠类型
    $type = $faker->randomElement(array_keys(CouponCode::$typeMap));

    // 根据取得的类型生成对于折扣
    if ($type === CouponCode::TYPE_FIXED){
        $value = random_int(1, 200);
        $minAmount = $value + 0.01;
    }else{
        $value = random_int(1, 50);
        if (random_int(1, 100) < 50){
            $minAmount = 0;
        }else{
            $minAmount = random_int(100, 1000);
        }
    }

    return [
        'name' => join(' ', $faker->words()),
        'code' => CouponCode::findAvailableCode(),
        'type' => $type,
        'value' => $value,
        'total' => 1000,
        'used' => 0,
        'min_amount' => $minAmount,
        'not_before' => null,
        'not_after' => null,
        'enabled' => true,
    ];
});
